package com.swpu.eduservice.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.eduservice.entity.UcenterMember;
import com.swpu.eduservice.entity.VO.LoginVo;
import com.swpu.eduservice.entity.VO.RegisterVo;
import com.swpu.eduservice.service.UcenterMemberService;
import com.swpu.servicebase.ExceptionHandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ygq
 * @since 2021-10-23
 */
@RestController
@CrossOrigin
@RequestMapping("/eduucenter/ucenter")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据id获取用户
    @PostMapping("/getInfo/{id}")
    public UcenterMember getMenm(@PathVariable String id){
        UcenterMember memeber = memberService.getById(id);
        return memeber;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R Count(@PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }
}

